package com.example.newproject_overseas.Screen.Main

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight.Companion.ExtraBold
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.newproject_overseas.R
import com.example.newproject_overseas.ViewModel.FormViewModel
import com.example.newproject_overseas.components.ShowDialog
import com.example.newproject_overseas.model.entity.form.CenobitismForm

@Composable
fun Home(navController: NavHostController, form_vm:FormViewModel = viewModel()) {

    //控制弹出框
    var showDialog by remember { mutableStateOf(false) }
    //表单组件
    var formInfo by remember { mutableStateOf<CenobitismForm?>(null) }

    if(showDialog){
        ShowDialog(
            title = "您有表单尚未完成 , 点击确定去查看!",
            confirmText = "确定",
            dismissText = "知道了",
            onConfirm = {
                if(formInfo?.formType == "OCR"){
                    navController.navigate("UploadCardScreen")
                }
                showDialog = false

            },
            onDismiss = { showDialog = false }
        );
    }


    LaunchedEffect(Unit){
        //获取一个未完成表单
        form_vm.cenobitismReq(
            navController,
            retData = { formObj ->
                showDialog = true
                formInfo   = formObj
                //Log.i("formObj" , formObj.toString())
                //print("11111111")

            }
        );
    }



    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(Color(0xffFFBE01), Color(0xffFFFCF5), Color(0xffFFFFFF)),
                    startY = 0f,
                    endY = Float.POSITIVE_INFINITY
                )
            )
    ) {//第一个引导语
        Row(modifier = Modifier.padding(start = 7.5.dp, top = 25.5.dp)) {
            Text(text = "Loan House", fontSize = 25.sp, color = Color.White)
        }
        //第二个引导语和图片
        Row(
            modifier = Modifier.fillMaxWidth()
        ) {
            //第二个引导语
            Row(
                modifier = Modifier
                    .padding(start = 7.5.dp, top = 33.5.dp)
                    .height(66.5.dp)
                    .width(235.5.dp)
            ) {
                Text(
                    text = "Quick loan.\n" + "With easy application process. Get the cash in a same day!",
                    fontSize = 15.sp,
                    color = Color.White,
                    maxLines = 3,
                    modifier = Modifier
                        .height(66.5.dp)
                        .width(235.5.dp)
                )
            }
            //引导语旁边的图片
            Row() {
                Image(
                    painter = painterResource(id = R.drawable.home_people),
                    contentDescription = null,
                    modifier = Modifier
                        .padding(start =20.dp)
                        .width(130.dp)
                        .height(123.dp)


                )
            }
        }
        //白色框

        Column(
            modifier = Modifier

                .fillMaxWidth()
                .height(216.dp)
                .padding(start = 7.5.dp, end = 7.5.dp)
                .background(Color.White)
        ) {
            Column() {
                Spacer(modifier = Modifier.height(20.dp))
                Text(
                    text = "Maximum Loan Amount",
                    fontSize = 17.5.sp,
                    lineHeight = 14.sp,
                    textAlign = TextAlign.Center,
                    color = Color(0xff161616),
                    modifier = Modifier.fillMaxWidth()
                )
                Row(
                    modifier= Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center
                ) {

                    Text(
                        text = "₹",
                        fontSize = 55.sp,
                        lineHeight = 6.5.sp,
                        color = Color(0xff161616)
                    )

                    Text(
                        text = "200,000",
                        fontSize = 55.sp,
                        lineHeight = 6.5.sp,
                        color = Color(0xff161616)
                    )

                }

                Spacer(modifier = Modifier.height(20.dp))
                Row() {
                    Button(
                        onClick = {},

                        colors = ButtonDefaults.buttonColors(
                            containerColor = Color(0xFFFFBD02),
                            contentColor = Color.White
                        ),
                        shape = RoundedCornerShape(12.5.dp),
                        modifier = Modifier

                            .fillMaxWidth()
                            .padding(horizontal = 53.5.dp)
                            .height(46.5.dp),
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Text(
                                text = "APPLY NOW",
                                modifier = Modifier.weight(1f),
                                textAlign = TextAlign.Center,
                                fontSize = 20.sp,
                            )
                        }
                    }
                }

            }
        }
        //第一个背景图
        Box(
            modifier = Modifier.fillMaxWidth().clickable { Log.i("---","no one") }
        ) {
            Image(
                painter = painterResource(id = R.drawable.home_backimg1),
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 7.5.dp)
                    .height(87.dp)
            )
            Box(
                modifier = Modifier
                    .fillMaxWidth()
//                    .border(1.dp, Color.Red, shape = RectangleShape)
//                    .padding(start = 21.dp, top = 14.dp),
            ) {
                Text(
                    text = "Improve information",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 43.dp, top = 20.dp),
                    fontSize = 18.sp,
                    lineHeight=17.sp,
                    fontWeight=ExtraBold,
                    color = Color(0xff161616)
                )
            }

            Box(
                modifier = Modifier
                    .fillMaxWidth()

//                    .padding(start = 21.dp, top = 14.dp),
            ) {
                Text(
                    text = "Improve your personal information",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 43.dp, top = 43.dp),
                    fontSize = 12.sp,
                    lineHeight=17.sp,

                    color = Color(0xffBDBDBD)
                )
            }

        }
//第二个背景图
        Box(
            modifier = Modifier.fillMaxWidth().clickable { Log.i("---","no two")}
        ) {
            Image(
                painter = painterResource(id = R.drawable.home_backimg2),
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 7.5.dp)
                    .height(87.dp)
            )
            Box(
                modifier = Modifier
                    .fillMaxWidth()

//                    .padding(start = 21.dp, top = 14.dp),
            ) {
                Text(
                    text = "Apply for approval",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 43.dp, top = 20.dp),
                    fontSize = 18.sp,
                    lineHeight=17.sp,
                    fontWeight=ExtraBold,
                    color = Color(0xff161616)
                )
            }

            Box(
                modifier = Modifier
                    .fillMaxWidth()
//                    .border(1.dp, Color.Red, shape = RectangleShape)
//                    .padding(start = 21.dp, top = 14.dp),
            ) {
                Text(
                    text = "Approval of your loan application",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 43.dp, top = 43.dp),
                    fontSize = 12.sp,
                    lineHeight=17.sp,

                    color = Color(0xffBDBDBD)
                )
            }

        }

    }
}



@Preview
@Composable
fun HomePreview() {
    val navController = rememberNavController()
    Home(navController)
}





